package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;

import org.mindrot.jbcrypt.BCrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dd.census.entity.SysUser;
import com.dd.census.service.SysUserService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.TokenHelper;
import com.dd.census.tools.resopnse.ResultResponse;
import com.dd.census.tools.resopnse.emums.ResultStatus;

@RestController
@RequestMapping(value = "census/sysUser")
public class SysUserController {

	private static final Logger logger = LoggerFactory.getLogger(SysUserController.class);

	@Autowired
	private SysUserService userService;

	@RequestMapping("select")
	public ResultResponse<Object> select(@RequestBody SysUser user, HttpServletRequest request) {
		try {
			return ResultResponse.buildSuccess(userService.selectPage(user));

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResultResponse<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return ResultResponse.buildSuccess(userService.getSysUserById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("update")
	public ResultResponse<Object> update(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 修改数据
			user.setUpdateBy(TokenHelper.getUserId(request));
			if (user.getPassword() != null && user.getPassword() != "") {
				String pass = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
				user.setPassword(pass);
			}
			return ResultResponse.buildSuccess(userService.update(user));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("updateNew")
	public ResultResponse<Object> updateNew(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			SysUser oldUser = userService.getSysUserById(user.getId());
			boolean p = BCrypt.checkpw(user.getPassword(), oldUser.getPassword());
			if (p) {
				// 修改数据
				user.setUpdateBy(TokenHelper.getUserId(request));
				String pass = BCrypt.hashpw(user.getNewPassword(), BCrypt.gensalt());
				user.setPassword(pass);
				return ResultResponse.buildSuccess(userService.updatePass(user));
			} else {
				throw new SysException(HttpStatus.FORBIDDEN);
			}
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
	
	@RequestMapping("enableAndDisable")
	public ResultResponse<Object> enableAndDisable(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			return ResultResponse.buildSuccess(userService.updatePass(user));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResultResponse<Object> insert(HttpServletRequest request, @RequestBody SysUser user) {

		try {
			// 插入数据
			user.setCreateBy(TokenHelper.getUserId(request));
			String pass = BCrypt.hashpw(user.getPassword(), BCrypt.gensalt());
			user.setPassword(pass);
			return ResultResponse.buildSuccess(userService.insert(user));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return ResultResponse.buildFailure(ResultStatus.FATAL_ERROR);
		}
	}
}
